﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace WebFabricator.org.Storage
{
    public class UriStorageProviderAttribute : Attribute
    {
       
        Type LocalStoreType { get; set; }

        public string Id
        {
            get;
            set;
        }

        public UriStorageProviderAttribute(Type localStore)
        {
            LocalStoreType = localStore;
            Id = AppDomain.CurrentDomain.FriendlyName;
        }

        internal static UriStore CreateLocalStore(Type type)
        {
           //System.Diagnostics.StackTrace frames = new System.Diagnostics.StackTrace();
           //Type type = frames.GetFrame(8).GetMethod().DeclaringType;
           //HttpContext.Current.ApplicationInstance.GetType()
           UriStore storage;
           UriStorageProviderAttribute attribute = GetAttribute(type);
           if (attribute == null || attribute.LocalStoreType == null)
               storage = new UriStore() { Id = attribute.Id };
           else
               storage = Activator.CreateInstance(attribute.LocalStoreType) as UriStore;
        
           if (storage != null)
           {
               storage.Id = attribute.Id;
           }
           return storage;
        }
            
        public static UriStorageProviderAttribute GetAttribute(Type type)
        {
            return type.GetCustomAttributes(typeof(UriStorageProviderAttribute), true).FirstOrDefault() as UriStorageProviderAttribute;
        }

    }
}
